//
//  MXBlackTableView.m
//  CatchBear
//
//  Created by 公司电脑 on 2019/1/24.
//  Copyright © 2019年 王启颖. All rights reserved.
//

#import "MXBlackTableView.h"
#import "MXBlackCell.h"
#import "User.h"

@interface MXBlackTableView () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *paramsArr;
@property (nonatomic, assign) int currentPage;
@property (strong, nonatomic) NSMutableDictionary *dicParams;
@end
@implementation MXBlackTableView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.bounds];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = MX_GRAY_COLOR;
        self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];
        
        [self addHeaderRefresh];
        [self refresh];
    }
    return self;
}

#pragma mark - 上下拉刷新事件 -
- (void)addHeaderRefresh{
    //默认【下拉刷新】
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    //默认【上拉加载】
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

-(void)refresh{
    self.currentPage = 1;
    
    [self.paramsArr removeAllObjects];
    [self getBlackUserByUseridRequest];
}

-(void)loadMore{
    self.currentPage ++;
    [self getBlackUserByUseridRequest];
}



#pragma mark - UITableViewDelegate -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.paramsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MXBlackCell *cell = [MXBlackCell cellWithTableView:tableView];
    User *model = self.paramsArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    User *model = self.paramsArr[indexPath.row];
    [self removeUserFromBlackRequest:model.user_id];
    
    [self.paramsArr removeObjectAtIndex:indexPath.row];

}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"移出";
}

#pragma mark - 懒加载 -
-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

- (NSMutableDictionary *)dicParams {
    if (!_dicParams) {
        _dicParams = [NSMutableDictionary dictionary];
    }
    [self initdicParams];
    return _dicParams;
}
- (void)initdicParams {
    _dicParams[@"pageNum"] = [@(self.currentPage) description];
    _dicParams[@"pageSize"] = @"3";
    _dicParams[@"user_id"] = [MXUserMessageTool message].user_id;
}

#pragma mark - 网络请求 -
//获取黑名单列表
-(void)getBlackUserByUseridRequest{
    [SVProgressHUD show];
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:getBlackUserByUserid params:self.dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (code == 0) {
            
            NSLog(@"%@",result);
            NSArray *messArr = result[@"message"];
            if ([messArr count] == 0) {
                self.tableView.mj_footer = nil;
            }else{
                self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
            }

            for (id res in result[@"message"]) {
                User *model = [User mj_objectWithKeyValues:res];
                [self.paramsArr addObject:model];
            }
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",error);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

//用户移出黑名单
-(void)removeUserFromBlackRequest:(NSString *)black_user_id{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_id"] = [MXUserMessageTool message].user_id;
    dic[@"black_user_id"] = black_user_id;
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:removeUserFromBlack params:dic isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];

        if (code == 0) {
            [SVProgressHUD showImage:NULL_IMAGE status:@"移除成功"];
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",error);
    }];
}

@end
